package com.sykj.why.controller.admin.pedigree;

import com.sykj.why.exception.WhyException;
import com.sykj.why.handler.Response;
import com.sykj.why.mongo.document.entity.ProjectCategoryT;
import com.sykj.why.mongo.enums.exception.ResultEnum;
import com.sykj.why.mongo.service.PedigreeTService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author Eleven
 * @date 2019/7/8
 * @desc 传承谱系访问控制层
 */

@RestController
@RequestMapping("/admin/pedigree")
@Slf4j
@Api(value = "/admin/pedigree",description = "传承谱系访问控制层")
public class PedigreeController {

    @Autowired
    private PedigreeTService pedigreeTService;

    /**
     * 添加传承谱系
     * @param projectCategoryT
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ApiOperation(value = "/add",notes = "添加传承谱系")
    public ResponseEntity add(@RequestBody ProjectCategoryT projectCategoryT){
        try {
            pedigreeTService.savePedigree(projectCategoryT);
            return Response.success("添加成功");
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("添加传承谱系出现未知异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 删除传承谱系
     * @param id
     * @return
     */
    @RequestMapping(value = "/remove/{id}",method = RequestMethod.GET)
    @ApiOperation(value = "/remove",notes = "删除传承谱系")
    public ResponseEntity remove(@PathVariable String id){
        try {
            pedigreeTService.removePedigree(id);
            return Response.success("删除成功");
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("删除传承谱系出现未知异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 修改传承谱系
     * @param projectCategoryT
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ApiOperation(value = "/update",notes = "修改传承谱系")
    public ResponseEntity update(@RequestBody ProjectCategoryT projectCategoryT){
        try {
            pedigreeTService.updatePedigree(projectCategoryT);
            return Response.success("修改成功");
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("修改传承谱系出现未知异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

}
